package com.example.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.example.entity.Choice;
import com.example.entity.Course;
import com.example.entity.Student;
import com.example.exception.CustomException;
import com.example.mapper.ChoiceMapper;
import com.example.mapper.CourseMapper;
import com.example.mapper.StudentMapper;
import com.example.service.ChoiceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

//你觉得什么时候时候用事务？---一个方法涉及多个写操作的时候，一般都要用事务！写--增删改
//spring事务失效的场景!!!---1.数据库要支持事务！ 2.抛出的异常不是RuntimeException 3.父方法没有事务，子方法有事务
@Service
public class ChoiceServiceImpl implements ChoiceService {
    @Resource
    private ChoiceMapper choiceMapper;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private CourseMapper courseMapper;


    /**
     * this.的时候事务为什么失效？
     * this是当前对象，事务生效是因为spring创建了代理对象
     */

    @Transactional
    @Override
    public void add(Course course) {
        //1.判断当前课程是否已经选满
        if (course.getAlreadyNum() >= course.getNum()) {
            throw new CustomException("课程已满");
        }
        //2.判断当前学生是否已经选过该课程
        //获取学生id
        Integer studentId = course.getStudentId();
        //获取课程id
        Integer courseId = course.getId();
        Choice choice = choiceMapper.selectByCourseIdAndStudentId(studentId, courseId);
        if (ObjectUtil.isNotNull(choice)) {
            throw new CustomException("请勿重复选课");
        }
        //3.添加学生的学分
        //获取学生
        Student student = studentMapper.selectById(studentId);
        student.setScore(student.getScore() + course.getScore());
        studentMapper.updateById(student);
        //4.添加课程的已选人数
        course.setAlreadyNum(course.getAlreadyNum() + 1);
        courseMapper.updateById(course);
        //5.添加选课信息
        Choice myChoice = new Choice();
        myChoice.setCourseId(courseId);
        myChoice.setStudentId(studentId);
        myChoice.setTeacherId(course.getTeacherId());
        myChoice.setName(course.getName());
        choiceMapper.add(myChoice);
    }

    @Override
    public PageInfo<Choice> selectPage(Integer pageNum, Integer pageSize, Choice choice) {
        List<Choice> list = null;
        PageHelper.startPage(pageNum, pageSize);
        //课程名称
        String name = choice.getName();
        //学生id
        Integer studentId = choice.getStudentId();
        //教师id
        Integer teacherId = choice.getTeacherId();
        //查询学生选课信息
        if (ObjectUtil.isNotEmpty(studentId)) {
            if (ObjectUtil.isNotEmpty(name)){
                list = choiceMapper.selectByStudentIdAndName(studentId, name);
            }else {
                list = choiceMapper.selectByStudentId(studentId);
            }
        }else if (ObjectUtil.isNotEmpty(teacherId)) {//查询教师的选课信息
            if (ObjectUtil.isNotEmpty(name)){
                list = choiceMapper.selectByTeacherIdAndName(teacherId, name);
            }else {
                list = choiceMapper.selectByTeacherId(teacherId);
            }
        }else {
            if (ObjectUtil.isNotEmpty(name)){
                list = choiceMapper.selectByName(name);
            }else {
                list = choiceMapper.selectAll();
            }
        }

        return PageInfo.of(list);
    }

    @Transactional
    @Override
    public void deleteById(Integer id) {
        //获取选课信息
        Choice choice = choiceMapper.selectById(id);
        //获取课程信息
        Course course = courseMapper.selectById(choice.getCourseId());
        //1.减去学生的学分
        Student student = studentMapper.selectById(choice.getStudentId());
        student.setScore(student.getScore()-course.getScore());
        studentMapper.updateById(student);
        //2.减去课程的已选人数
        course.setAlreadyNum(course.getAlreadyNum()-1);
        courseMapper.updateById(course);
        //3.删除选课信息
        choiceMapper.deleteById(id);
    }
}
